package org.secangkirkopipanas.gxtcore.server.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

public class MD5 {

	private static final String ALGORITHM = "MD5";
	private static Logger _logger = Logger.getLogger(MD5.class);

	public static String getDigest(String password) {
		String digest = null;
		try {
			MessageDigest md = MessageDigest.getInstance(ALGORITHM);
			md.update(password.getBytes());
			byte[] result = md.digest();
			digest = "";
			for (int i = 0; i < result.length; i++) {
				digest += toHexString(result[i]);
			}
		} catch (NoSuchAlgorithmException e) {
			_logger.error(e.getMessage());
		}
		return digest;
	}

	private static String toHexString(byte b) {
		int value = (b & 0x7F) + (b < 0 ? 128 : 0);
		String ret = (value < 16 ? "0" : "");
		ret += Integer.toHexString(value).toUpperCase();
		return ret;
	}
}
